//引入express
const express = require('express');
const {response, request} = require("express");
//创建应用对象
const app = express();
//路由规则
app.get('/server',(request,response)=> {
    //跨域解决
    response.setHeader('Access-Control-Allow-Origin','*');
    response.send("HELLO AJAX");
});
//接收所有的参数
app.all('/server',(request,response)=> {
    //跨域解决
    response.setHeader('Access-Control-Allow-Origin','*');
    //允许自定义请求头
    response.setHeader('Access-Control-Allow-Headers','*');
    response.send("HELLO AJAX POST");
});
app.all('/server-json',(request,response)=> {
    //跨域解决
    response.setHeader('Access-Control-Allow-Origin','*');
    //允许自定义请求头
    response.setHeader('Access-Control-Allow-Headers','*');
    //定义一个对象
    const data = {
        'name':'cty',
        'age':'18',
        'gender':'male'
    };
    var str = JSON.stringify(data);
    //send() 只能发送字符串
    response.send(str);
});

app.get('/ie',(request,response)=> {
    //跨域解决
    response.setHeader('Access-Control-Allow-Origin','*');
    response.send("HELLO ie -5");
});
app.get('/delay',(request,response)=> {
    //跨域解决
    response.setHeader('Access-Control-Allow-Origin','*');
    setTimeout(function ( ) {
        response.send("HELLO delay");
    },3000)

});
app.all('/jquery-server',(request,response)=> {
    //跨域解决
    response.setHeader('Access-Control-Allow-Origin','*');
    var data = {'name':'cty','age':18};
    response.send(JSON.stringify(data));
});
app.all('/axios-server',(request,response)=> {
    //跨域解决
    response.setHeader('Access-Control-Allow-Origin','*');
    response.setHeader('Access-Control-Allow-Headers','*');
    var data = {'name':'cty','age':18};
    response.send(JSON.stringify(data));
});
app.all('/fetch-server',(request,response)=> {
    //跨域解决
    response.setHeader('Access-Control-Allow-Origin','*');
    response.setHeader('Access-Control-Allow-Headers','*');
    var data = {'name':'cty','age':18};
    response.send(JSON.stringify(data));
});
//监听端口
app.listen(8000,()=> {
    console.log("服务已启动， 8000 端口正在监听");
})